using NUnit.Framework;
using TDUnit.TestDataParsers.XmlParser;

namespace TDUnit.Tests
{
    [TestFixture]
    public class TableElementParserTests : XmlTestDataParserBase
    {
        #region Parse Table element tests
        [Test]
        public void ParserCanParseTableElement()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='True' /></TestData>";
            var ds = Parse(dataset);
            var table = ds.GetTable("TableName");
            Assert.IsNotNull(table);
            Assert.AreEqual("TableName", table.Name);
            Assert.AreEqual("PrimaryKey", table.PrimaryKeyColumnName);
            Assert.IsTrue(table.IsPrimaryKeyAutoIdentity);
        }

        [Test]
        [ExpectedException(typeof(XmlTestDataParserException))]
        public void MissingTableNameOnTableElementCausesException()
        {
            const string dataset = @"<TestData><Table PrimaryKey='PrimaryKey' IsAutoIdentity='True' /></TestData>";
            Parse(dataset);
        }

        [Test]
        [ExpectedException(typeof(XmlTestDataParserException))]
        public void MissingPrimaryKeyOnTableElementCausesException()
        {
            const string dataset = @"<TestData><Table Name='TableName' IsAutoIdentity='True' /></TestData>";
            Parse(dataset);
        }

        [Test]
        public void MissingIsAutoIdentityOnTableElementDefaultsToFalse()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' /></TestData>";
            var ds = Parse(dataset);
            Assert.IsFalse(ds.GetTable("TableName").IsPrimaryKeyAutoIdentity);
        }

        [Test]
        [ExpectedException(typeof(XmlTestDataParserException))]
        public void IsAutoIdentityOnTableElementIsSetToInvalidValueCausesException()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='AnInvalidBoolean' /></TestData>";
            Parse(dataset);
        }

        [Test]
        public void IsAutoIdentitySetToZeroWillBeSetToFalse()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='0' /></TestData>";
            var ds = Parse(dataset);
            Assert.IsFalse(ds.GetTable("TableName").IsPrimaryKeyAutoIdentity);
        }

        [Test]
        public void AliasIsSetOnTableIfTableContainsAliasAttribute()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='0' Alias='TableAlias' /></TestData>";
            var ds = Parse(dataset);
            Assert.AreEqual("TableAlias", ds.GetTable("TableName").Alias);
        }

        #endregion
    }
}